// 依赖
import axios from 'axios';
import { EventEmitter } from 'events';
import { SyntheticEvent } from 'react';

// 常量

//个位数前面加0
export const $AddZero = (num: number) => {
    return `${num > 9 ? "" : "0"}${num}`;
}

// 科学计数法
export const $ScientificNotation = (num: number, separators: string = ",") => {
    num ??= 0
    return num
        .toFixed(2)
        .replace(/\d+/, (n) => {
            return n.replace(/(\d)(?=(?:\d{3})+$)/g, `$1${separators}`);
        });
}

// 阻止冒泡
export const $StopPropagation = (e: SyntheticEvent) => {
    e.stopPropagation()
    e.nativeEvent.stopImmediatePropagation()
}

/**
 * 
 * @brief 跨组件通讯广播
 * @param
 * @return 
 * @note 发送：.emit(name,data)
         接收：.addListener((res)=>{}) 
         销毁 .removeAllListeners(name)
 * @warning 
 */
export const $eventBus = new EventEmitter();

export const $EventBus_on = (dict: object) => {
    Object.keys(dict).forEach((item: string) => {
        $eventBus.on(item, dict[item])
    })
}

export const $EventBus_off = (dict: object) => {
    Object.keys(dict).forEach((item: string) => {
        $eventBus.off(item, dict[item])
    })
}

